| 37 | extern PyObject* cpp_astensor1d; |
| 38 | |
| 39 | struct Tensor { |
| 40 | private: |
| 41 | ValueRef m_data; |
| 42 | std::string m_name; |
| 43 | |
| 44 | public: |
| 45 | using Handle = interpreter::Interpreter::Handle; |
| 46 | |
| 47 | inline explicit Tensor(ValueRef data) : m_data{data} {} |
| 48 | |
| 49 | ~Tensor() = default; |
| 50 | |
| 51 | inline Tensor copy() { return Tensor(imperative::apply(DupTensor(), data())[0]); } |
| 52 | |
| 53 | inline DType dtype() { return *data().dtype(); } |
| 54 | inline CompNode comp_node() { return *data().device(); } |
| 55 | inline std::optional<ValueShape> shape() { |
| 56 | auto shape = data().shape(); |
| 57 | if (!shape) { |
| 58 | return {}; |
| 59 | } |
| 60 | return *shape; |
| 61 | } |
| 62 | inline Format format() { return *data().format(); } |
| 63 | inline void set_format(std::string format) { |
| 64 | if (!format.empty()) { |
| 65 | m_data = imperative::apply(SetFormat(format), m_data)[0]; |
| 66 | } |
| 67 | } |
| 68 | inline void as_format(std::string format) { |
| 69 | if (!format.empty()) { |
| 70 | m_data = imperative::apply(AsFormat(format), m_data)[0]; |
| 71 | } |
| 72 | } |
| 73 | inline HostValue::ref_t numpy() { return data().numpy(); } |
| 74 | inline void reset(ValueRef value) { |
| 75 | m_data = value; |
| 76 | if (!m_name.empty()) { |
| 77 | set_name(m_name); |
| 78 | } |
| 79 | } |
| 80 | inline ValueRef data() const { return m_data.unwrap(); } |
| 81 | bool is_scalar() { return data().is_scalar(); } |
| 82 | inline std::string name() { return m_name; } |
| 83 | inline size_t value_id() { return m_data.id(); } |
| 84 | inline void set_name(std::string name) { |
| 85 | m_name = name; |
| 86 | if (!name.empty()) { |
| 87 | auto output = imperative::apply(RenameValue(name), m_data)[0]; |
| 88 | m_data = output; |
| 89 | } |
| 90 | } |
| 91 | }; |
| 92 | |
| 93 | struct TensorWrapper { |
| 94 | public: |
no outgoing calls