Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[jit] Support torch.save for saving values during execution #18154

Closed
wants to merge 27 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions aten/src/ATen/core/interned_strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ namespace c10 {
_(aten, len) \
_(aten, list) \
_(aten, wait) \
_(aten, save) \
_(aten, ord) \
_(prim, unchecked_unwrap_optional)\
FORALL_ATEN_BASE_SYMBOLS(_) \
Expand Down
63 changes: 63 additions & 0 deletions test/test_jit.py
Original file line number Diff line number Diff line change
Expand Up @@ -12037,6 +12037,69 @@ def fn(x=None):
self.checkScript(fn, ((3, 4),))
self.checkScript(fn, ())

def _test_pickle_checkpoint(self, device):
driazati marked this conversation as resolved.
Show resolved Hide resolved
with TemporaryFileName() as fname:
class M(torch.jit.ScriptModule):
__constants__ = ['fname']

def __init__(self, tensor):
super(M, self).__init__()
self.fname = fname
self.tensor = torch.nn.Parameter(tensor)

@torch.jit.script_method
def forward(self, x):
y = self.tensor + x
torch.save(y, self.fname)
return y

param = torch.randn(2, 2).to(device)
input = torch.randn(2, 2).to(device)
m = M(param)
m(input)
with open(fname, "rb") as handle:
loaded_tensor = torch.load(fname)
self.assertEqual(loaded_tensor, input + param)

def _test_pickle_checkpoint_views(self, device):
with TemporaryFileName() as fname:
class M(torch.jit.ScriptModule):
__constants__ = ['fname']

def __init__(self, tensor):
super(M, self).__init__()
self.fname = fname
self.tensor = torch.nn.Parameter(tensor)

@torch.jit.script_method
def forward(self, x):
y = self.tensor + x
y_view = y.view(4)
torch.save((y, y_view, y), self.fname)
return y

param = torch.randn(2, 2).to(device)
input = torch.randn(2, 2).to(device)
m = M(param)
m(input)
with open(fname, "rb") as handle:
loaded_y, loaded_y_view, loaded_y_2 = torch.load(fname)
self.assertEqual(loaded_y, input + param)
with torch.no_grad():
loaded_y_view[1] += 20
# assert that loaded_y changed as well
self.assertEqual(loaded_y.view(4), loaded_y_view)
self.assertEqual(loaded_y_2.view(4), loaded_y_view)

@unittest.skipIf(not RUN_CUDA, "no CUDA")
def test_pickle_checkpoint_cuda(self):
self._test_pickle_checkpoint('cuda')
self._test_pickle_checkpoint_views('cuda')

def test_pickle_checkpoint(self):
self._test_pickle_checkpoint('cpu')
self._test_pickle_checkpoint_views('cpu')

def test_split(self):
def split_two(tensor):
a, b, c = torch.split(tensor, 2, dim=1)
Expand Down
25 changes: 5 additions & 20 deletions torch/csrc/jit/export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -701,29 +701,12 @@ void ScriptModuleSerializer::convertAndWriteTensor(

tensor_proto->set_requires_grad(tensor.requires_grad());

uint64_t record_size = tensor.element_size() * tensor.storage().size();
auto* key = tensor.storage().unsafeGetStorageImpl();

auto storage_it = storageMap.find(key);
if (storage_it == storageMap.end()) {
at::Tensor storage_tensor = tensor;
// TODO HIP support
if (tensor.storage().device_type() == at::DeviceType::CUDA) {
// NB: This new tensor is created to support cuda tensors.
// Storages can be mutated when converting tensors from cuda to cpu,
// and we need a cpu tensor to copy data from.
storage_tensor = at::empty({0}, tensor.options())
.set_(
tensor.storage(),
/* storageOffset = */ 0,
/* size = */
{static_cast<int64_t>(tensor.storage().size())},
/* stride = */ {1})
.cpu();
AT_ASSERT(
storage_tensor.element_size() * storage_tensor.storage().size() ==
record_size);
}
uint64_t record_size;
at::Tensor storage_tensor;
std::tie(storage_tensor, record_size) = getWriteableTensor(tensor);
std::string name = "tensors/" + std::to_string(tensor_id);
writer_.writeRecord(name, storage_tensor.storage().data(), record_size);
storage_it = storageMap.insert({key, name}).first;
Expand All @@ -750,9 +733,11 @@ void ScriptModuleSerializer::writeTensorTable(torch::ModelDef* model_def) {
void ScriptModuleSerializer::writeAttributeTable() {
Pickler pickler(&tensor_table_);
pickler.start();
pickler.startTuple();
for (const IValue& ivalue : attribute_table_) {
pickler.addIValue(ivalue);
}
pickler.endTuple();
pickler.finish();
writer_.writeRecord(
"attributes.pkl", pickler.stack().data(), pickler.stack().size());
Expand Down
1 change: 1 addition & 0 deletions torch/csrc/jit/ir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,7 @@ bool Node::hasSideEffects() const {
case prim::RaiseException:
case prim::SetAttr:
case aten::warn:
case aten::save:
case aten::manual_seed:
case prim::AddStatValue:
case prim::TimePoint:
Expand Down
Loading