const torch::jit::script::Module& module,
Predicate predicate) {
for (const auto& parameter : module.get_parameters()) {
- AT_ASSERT(predicate(parameter.slot()->toTensor()));
+ AT_ASSERT(predicate(parameter.slot().value().toTensor()));
}
for (const auto& child : module.get_modules()) {
- check_all_parameters(module, predicate);
+ check_all_parameters(*child, predicate);
}
}
} // namespace helpers
"'");
// clang-format off
auto read_param = is_buffer ? buffer : param;
- auto read_tensor = read_param->slot()->toTensor();
+ auto read_tensor = read_param->slot().value().toTensor();
AT_CHECK(
bool(buffer) == is_buffer,
"Expected deserialized tensor for key '", key,
}
pickler.finish();
writer_.writeRecord(
- "attributes.pkl", pickler.stack().data(), pickler.stack().size());
+ "attributes.pkl", pickler.stack().data(), pickler.stack().size());
}
void ScriptModuleSerializer::convertModule(
attribute_def->set_name(attribute.name());
attribute_def->set_type(attribute.type()->python_str());
- attribute_table_.push_back(*attribute.slot());
+ attribute_table_.push_back(attribute.slot().value());
attribute_def->set_id(attribute_table_.size() - 1);
}
bool is_parameter) {
param_def->set_name(param.name());
param_def->set_is_buffer(is_parameter);
- param_def->set_tensor_id(addTensor(param.slot()->toTensor()));
+ param_def->set_tensor_id(addTensor(param.slot().value().toTensor()));
}
// Pretty printing for ONNX
auto& p = parameters[i];
py::tuple r(2);
result[i] = std::make_tuple(
- p.name(), autograd::as_variable_ref(p.slot()->toTensor()));
+ p.name(),
+ autograd::as_variable_ref(p.slot().value().toTensor()));
}
return result;
})
for (size_t i = 0; i < attributes.size(); ++i) {
auto& buffer = attributes[i];
py::tuple r(3);
- IValue v = *buffer.slot();
+ IValue v = buffer.slot().value();
result[i] = std::make_tuple(
buffer.name(), buffer.type(), toPyObject(std::move(v)));
}
gatherParametersAndBuffers(parameters, *self);
Stack inputs = toStack(input_tuple);
for (const Slot& param : parameters) {
- inputs.emplace_back(*param);
+ inputs.emplace_back(param.value());
}
auto graph = tracer::createGraphByTracing(
func,
[](Method& m) {
std::vector<at::Tensor> tensors;
for (auto& t : m.initial_ivalues()) {
- tensors.push_back(t->toTensor());
+ tensors.push_back(t.value().toTensor());
}
return tensors;
})
" from a raw graph. File a bug report";
}
// TODO: preserve the type information so we don't have to infer it here
- auto type = incompleteInferTypeFrom(*member);
+ auto type = incompleteInferTypeFrom(member.value());
matched_schema->inputs.push_back(
caller->get_or_add_attribute(type, member));
}
// Then convert every of our parameters.
for (auto& parameter : get_parameters()) {
// Need to access the `at::Tensor` as a `Variable` here.
- autograd::Variable variable = parameter.slot()->toTensor();
+ autograd::Variable variable = parameter.slot().value().toTensor();
at::Tensor data = variable.data();
// Use the data's original device or dtype if not supplied here.
auto new_data = data.to(
void run(Stack& stack) {
for (auto input : initial_ivalues_) {
- push(stack, *input);
+ push(stack, input.value());
}
get_executor().run(stack);
}
std::shared_ptr<Graph> graph_for(Stack inputs) {
for (auto tp : initial_ivalues_) {
- inputs.emplace_back(*tp);
+ inputs.emplace_back(tp.value());
}
return get_executor().graphFor(inputs);
}
return graph()->inputs().size() - initial_ivalues_.size();
}
TORCH_API Value* get_or_add_parameter(Slot slot) {
- AT_ASSERT(slot->isTensor());
+ AT_ASSERT(slot.value().isTensor());
return get_or_add_attribute(TensorType::get(), slot);
}
stack.emplace_back(std::move(i));
}
for (const Slot& inp : initial_ivalues_) {
- stack.push_back(*inp);
+ stack.push_back(inp.value());
}
setInputTensorTypes(*retval, stack);
PropagateInputShapes(retval);
bool propagate = true) {
auto retval = graph_->copy();
for (auto inp : initial_ivalues_) {
- if (inp->isTensor()) {
- inputs.push_back(inp->toTensor());
+ if (inp.value().isTensor()) {
+ inputs.push_back(inp.value().toTensor());
}
}
if (propagate) {
void register_buffer(const std::string& name, autograd::Variable v) {
if (auto b = find_attribute(name)) {
AT_ASSERT(b->type()->isSubtypeOf(TensorType::get()));
- *b->slot() = v;
+ b->slot().setValue(v);
return;
}
insert(
return;
}
if (auto p = find_parameter(name)) {
- *p->slot() = v;
+ p->slot().setValue(v);
return;
}
insert(
}
void set_parameter(const std::string& name, at::Tensor v) {
- *parameter_slot(name) = std::move(v);
+ parameter_slot(name).setValue(std::move(v));
}
autograd::Variable get_parameter(const std::string& name) const {
- return autograd::as_variable_ref(parameter_slot(name)->toTensor());
+ return autograd::as_variable_ref(parameter_slot(name).value().toTensor());
}
IValue get_attribute(const std::string& name) const {
- return *attributes_[get_offset(name, EntityType::ATTRIBUTE)].slot();
+ return attributes_[get_offset(name, EntityType::ATTRIBUTE)].slot().value();
}
autograd::Variable get_buffer(const std::string& name) const {
/// True if the module is in training mode.
bool is_training() {
if (auto p = find_buffer("training")) {
- return p->slot()->toTensor().item<int64_t>() == 1;
+ return p->slot().value().toTensor().item<int64_t>() == 1;
}
// We are in training mode by default
return true;
for (auto& param : get_parameters()) {
curr->register_parameter(
param.name(),
- param.slot()->toTensor(),
+ param.slot().value().toTensor(),
/*is_buffer=*/false);
parameter_remap[param.slot()] = curr->parameter_slot(param.name());
}
if (!attr.type()->isSubtypeOf(TensorType::get())) {
continue;
}
- curr->register_buffer(attr.name(), attr.slot()->toTensor());
+ curr->register_buffer(attr.name(), attr.slot().value().toTensor());
parameter_remap[attr.slot()] = curr->find_buffer(attr.name())->slot();
}
for (auto& mod : get_modules()) {
// a stable location that can hold an IValue.
// Currently this is internally implemented as a pointer, but when
-// modules become first-class this will be a pair of <module_ivalue, slot_number>
+// modules become first-class this will be a pair of <module_ivalue,
+// slot_number>
struct Slot {
friend struct NamedIValue;
- Slot()
- : slot_(nullptr) {}
- Slot(at::IValue* slot)
- : slot_(slot) {}
- at::IValue& operator*() const {
- return *slot_;
- }
- at::IValue* operator->() const {
- return slot_;
- }
+ Slot() : slot_(nullptr) {}
+ Slot(at::IValue* slot) : slot_(slot) {}
+
bool operator==(const Slot& rhs) const {
return slot_ == rhs.slot_;
}
-private:
+ void setValue(at::IValue v) {
+ *slot_ = std::move(v);
+ }
+ const at::IValue& value() const {
+ return *slot_;
+ }
+
+ private:
at::IValue* slot_;
friend struct std::hash<Slot>;
};
-}}}
+} // namespace script
+} // namespace jit
+} // namespace torch
// slots are hashable, because they are often used as keys in maps
// for remapping uses of a slot from one model to another
namespace std {
- template <>
- struct hash<torch::jit::script::Slot> {
- size_t operator()(const torch::jit::script::Slot& s) const noexcept {
- return std::hash<at::IValue*>{}(s.slot_);
- }
- };
+template <>
+struct hash<torch::jit::script::Slot> {
+ size_t operator()(const torch::jit::script::Slot& s) const noexcept {
+ return std::hash<at::IValue*>{}(s.slot_);
+ }
+};
} // namespace std