From 2ed99547260f976b13957cfef4eea0da5a20752e Mon Sep 17 00:00:00 2001 From: Henry Hu Date: Fri, 4 Aug 2023 00:53:26 -0700 Subject: [PATCH 1/2] Move tensor to CPU before converting to string for unpickle Summary: The loaded tensor could be on CUDA, whose data cannot to converted to a std::string. This change moves tensor to CPU first. Then data can be converted. Reviewed By: mortzur Differential Revision: D48057806 fbshipit-source-id: ad495bb2e84f9aab5120d5462d4d1c381719d014 --- torchtext/csrc/register_torchbindings.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/torchtext/csrc/register_torchbindings.cpp b/torchtext/csrc/register_torchbindings.cpp index ee509668cf..6f13bcb044 100644 --- a/torchtext/csrc/register_torchbindings.cpp +++ b/torchtext/csrc/register_torchbindings.cpp @@ -67,6 +67,7 @@ TORCH_LIBRARY_FRAGMENT(torchtext, m) { }, // __setstate__ [](torch::Tensor state) -> c10::intrusive_ptr { + state = state.to(at::kCPU); auto* data = static_cast(state.data_ptr()); auto numel = state.size(0); return c10::make_intrusive(std::string(data, numel)); From 24ca2e68353b8051150df3e3e1bfb84e28fcd091 Mon Sep 17 00:00:00 2001 From: Richard Barnes Date: Tue, 5 Sep 2023 17:54:21 -0700 Subject: [PATCH 2/2] Del `(object)` from 10 inc pytorch/tensorboardX/tensorboardX/record_writer.py Summary: Python3 makes the use of `(object)` in class inheritance unnecessary. Let's modernize our code by eliminating this. Reviewed By: meyering Differential Revision: D48958009 fbshipit-source-id: aa42e55a827d83aadf1b10b2de79317c3761ab7a --- torchtext/data/utils.py | 2 +- torchtext/vocab/vectors.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/torchtext/data/utils.py b/torchtext/data/utils.py index a1b53ab559..89a72ea455 100644 --- a/torchtext/data/utils.py +++ b/torchtext/data/utils.py @@ -228,7 +228,7 @@ def _get_ngrams(n): yield " ".join(x) -class RandomShuffler(object): +class RandomShuffler: """Use random functions while keeping track of the random state to make it reproducible and deterministic.""" diff --git a/torchtext/vocab/vectors.py b/torchtext/vocab/vectors.py index af60c4f60b..3743207557 100644 --- a/torchtext/vocab/vectors.py +++ b/torchtext/vocab/vectors.py @@ -31,7 +31,7 @@ def _infer_shape(f): return num_lines, vector_dim -class Vectors(object): +class Vectors: def __init__(self, name, cache=None, url=None, unk_init=None, max_vectors=None) -> None: """ Args: