1 #include <ATen/core/dispatch/KernelRegistration.h> 2 #include "caffe2/operators/experimental/c10/schemas/expand_dims.h" 3 #include "caffe2/utils/math.h" 4 #include "caffe2/core/tensor.h" 12 std::vector<int64_t> dims;
13 bool initialized =
false;
16 template <
class DataType>
17 void expand_dims_op_cpu_impl(
20 ArrayRef<int64_t> dims,
22 Tensor input{C10Tensor(input_)};
23 Tensor output{C10Tensor(output_)};
25 if (!cache->initialized) {
26 cache->dims = dims.vec();
27 auto originalSize = cache->dims.size();
28 CAFFE_ENFORCE(originalSize > 0,
"Parameter `dims` must be provided.");
29 std::sort(cache->dims.begin(), cache->dims.end());
31 std::unique(cache->dims.begin(), cache->dims.end()), cache->dims.end());
32 if (cache->dims.size() < originalSize) {
33 LOG(WARNING) <<
"Parameter `dims` has repeated dimensions.";
36 cache->dims.front() >= 0,
"Dimension ids must be non-negative.");
37 cache->initialized =
true;
40 output.CopyFrom(input);
41 if (cache->dims.empty()) {
45 auto newDims = input.sizes().vec();
47 input.sizes().size() + cache->dims.size(),
48 cache->dims.back() + 1,
49 "Input needs at least ",
50 (1 + cache->dims.back() - cache->dims.size()),
51 " dimensions given `dims`.");
52 for (
const auto dim : cache->dims) {
53 newDims.insert(newDims.begin() + dim, 1);
55 output.Reshape(newDims);
61 C10_REGISTER_KERNEL(caffe2::ops::ExpandDims)
62 .withCache<caffe2::Cache>()
63 .kernel<decltype(caffe2::expand_dims_op_cpu_impl<float>), &caffe2::expand_dims_op_cpu_impl<float>>()
64 .dispatchKey(CPUTensorId());
Tensor class holds a shared pointer to the implementation TensorImpl, redirects API calls to TensorIm...
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
To register your own kernel for an operator, do in one (!) cpp file: C10_REGISTER_KERNEL(OperatorHand...
A kernel can keep around a cache to have better performance when it's called multiple times...