CUDNN Frontend API  8.3.0
cudnn_frontend_Filters.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #pragma once
24 
25 #include <cudnn.h>
26 
27 namespace cudnn_frontend {
28 
29 // If filter_fn returns true
30 // The engine config will be filtered out and will
31 // not be part of the to list.
32 static void
33 filter(EngineConfigList &from, EngineConfigList &to, std::function<bool(cudnnBackendDescriptor_t)> filter_fn) {
34  auto p = std::stable_partition(from.begin(), from.end(), [filter_fn](ManagedOpaqueDescriptor &p) {
35  return filter_fn(const_cast<cudnnBackendDescriptor_t>(p->get_backend_descriptor()));
36  });
37  // range insert with move
38  to.insert(to.end(), std::make_move_iterator(p), std::make_move_iterator(from.end()));
39  // erase the moved-from elements.
40  from.erase(p, from.end());
41 }
42 
43 template <cudnnBackendNumericalNote_t NUMERIC_NOTE>
44 bool
45 hasNumericalNote(cudnnBackendDescriptor_t engine_config) {
46  bool hasNumerics = false;
47  auto status = CUDNN_STATUS_SUCCESS;
48  ManagedOpaqueDescriptor engine = make_shared_backend_pointer(CUDNN_BACKEND_ENGINE_DESCRIPTOR);
49  cudnnBackendDescriptor_t engine_ = engine->get_backend_descriptor();
50  int64_t engine_count = -1;
51  status = cudnnBackendGetAttribute(
52  engine_config, CUDNN_ATTR_ENGINECFG_ENGINE, CUDNN_TYPE_BACKEND_DESCRIPTOR, 1, &engine_count, &engine_);
53  if (status == CUDNN_STATUS_SUCCESS) {
54  cudnnBackendNumericalNote_t notes[CUDNN_NUMERICAL_NOTE_TYPE_COUNT];
55  int64_t elem_count = 0;
56  cudnnBackendGetAttribute(engine->get_backend_descriptor(),
57  CUDNN_ATTR_ENGINE_NUMERICAL_NOTE,
58  CUDNN_TYPE_NUMERICAL_NOTE,
59  CUDNN_NUMERICAL_NOTE_TYPE_COUNT,
60  &elem_count,
61  notes);
62  if (std::any_of(
63  notes, notes + elem_count, [](cudnnBackendNumericalNote_t note) { return note == NUMERIC_NOTE; })) {
64  hasNumerics = true;
65  }
66  }
67  return hasNumerics;
68 }
69 
70 #if (CUDNN_VERSION >= 8200)
71 template <cudnnBackendBehaviorNote_t BEHAVIOR_NOTE>
72 bool
73 hasBehaviorNote(cudnnBackendDescriptor_t engine_config) {
74  bool hasBehavior = false;
75  auto status = CUDNN_STATUS_SUCCESS;
76  ManagedOpaqueDescriptor engine = make_shared_backend_pointer(CUDNN_BACKEND_ENGINE_DESCRIPTOR);
77  cudnnBackendDescriptor_t engine_ = engine->get_backend_descriptor();
78  int64_t engine_count = -1;
79  status = cudnnBackendGetAttribute(
80  engine_config, CUDNN_ATTR_ENGINECFG_ENGINE, CUDNN_TYPE_BACKEND_DESCRIPTOR, 1, &engine_count, &engine_);
81  if (status == CUDNN_STATUS_SUCCESS) {
82  cudnnBackendBehaviorNote_t notes[CUDNN_BEHAVIOR_NOTE_TYPE_COUNT];
83  int64_t elem_count = 0;
84  cudnnBackendGetAttribute(engine->get_backend_descriptor(),
85  CUDNN_ATTR_ENGINE_BEHAVIOR_NOTE,
86  CUDNN_TYPE_BEHAVIOR_NOTE,
87  CUDNN_BEHAVIOR_NOTE_TYPE_COUNT,
88  &elem_count,
89  notes);
90  if (std::any_of(
91  notes, notes + elem_count, [](cudnnBackendBehaviorNote_t note) { return note == BEHAVIOR_NOTE; })) {
92  hasBehavior = true;
93  }
94  }
95  return hasBehavior;
96 }
97 #endif
98 }
static auto filter(Predicate pred, executionPlans_t &plans) -> executionPlans_t
static ManagedOpaqueDescriptor make_shared_backend_pointer(cudnnBackendDescriptorType_t type)
bool hasNumericalNote(cudnnBackendDescriptor_t engine_config)
std::shared_ptr< OpaqueBackendPointer > ManagedOpaqueDescriptor
std::vector< ManagedOpaqueDescriptor > EngineConfigList