forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input_metadata.h
48 lines (35 loc) · 1.05 KB
/
input_metadata.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#pragma once
#include <ATen/ATen.h>
#include <cstdint>
namespace torch { namespace autograd {
/// A tensor's type and shape. Each Function records the required type and
/// shape of its inputs. If is_valid() is false, then the corresponding input
/// is not used and may be an undefined tensor.
struct InputMetadata {
InputMetadata() = default;
InputMetadata(const at::Type& type, at::IntArrayRef shape, const int64_t device)
: type_{&type} , shape_{shape}, device_{device} { }
InputMetadata(const at::Tensor& t)
: InputMetadata(t.type(), t.sizes(), t.is_cuda() ? t.get_device() : - 1) { }
bool is_valid() const {
return type_ != nullptr;
}
const at::Type& type() const {
AT_ASSERT(type_);
return *type_;
}
at::IntArrayRef shape() const {
return shape_;
}
int64_t device() const {
return device_;
}
at::Tensor zeros_like() const {
return at::zeros(shape_, type_->options(static_cast<int32_t>(device_)));
}
private:
const at::Type* type_ = nullptr;
at::DimVector shape_;
const int64_t device_ = -1;
};
}}