From 54ea061ffe8c5f208e87221c461d61cafb1b93da Mon Sep 17 00:00:00 2001 From: xingjinliang Date: Thu, 24 Mar 2022 07:01:50 +0000 Subject: [PATCH 1/7] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E9=A2=84=E5=A4=84=E7=90=86=E7=AE=97=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NeuMF_ID0351_for_PyTorch/prefetcher.py | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 PyTorch/dev/cv/image_classification/NeuMF_ID0351_for_PyTorch/prefetcher.py diff --git a/PyTorch/dev/cv/image_classification/NeuMF_ID0351_for_PyTorch/prefetcher.py b/PyTorch/dev/cv/image_classification/NeuMF_ID0351_for_PyTorch/prefetcher.py new file mode 100644 index 0000000000..5e05ead500 --- /dev/null +++ b/PyTorch/dev/cv/image_classification/NeuMF_ID0351_for_PyTorch/prefetcher.py @@ -0,0 +1,67 @@ +# Copyright (c) 2020 Huawei Technologies Co., Ltd +# Copyright (c) 2019, Facebook CORPORATION. +# All rights reserved. +# +# Licensed under the BSD 3-Clause License (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://opensource.org/licenses/BSD-3-Clause +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import torch + + +class Prefetcher(object): + """Prefetcher using on npu device. + + Origin Code URL: + https://github.com/implus/PytorchInsight/blob/master/classification/imagenet_fast.py#L280 + + Args: + loder (torch.utils.data.DataLoader or DataLoader like iterator): + Using to generate inputs after preprocessing. + stream (torch.npu.Stream): Default None. + Because of the limitation of NPU's memory mechanism, + if prefetcher is initialized repeatedly during training, + a defined stream should be introduced to prevent memory leakage; + if prefetcher is initialized only once during training, + a defined stream is not necessary. + + Returns: + float: tensors of shape (k, 5) and (k, 1). Labels are 0-based. + """ + + def __init__(self, loader, stream=None): + self.loader = iter(loader) + self.stream = stream if stream is not None else torch.npu.Stream() + self.preload() + + def preload(self): + try: + self.user, self.item, self.rating = next(self.loader) + assert isinstance(self.user, torch.IntTensor) + self.rating = self.rating.float() + except StopIteration: + self.user = None + self.item = None + return + + with torch.npu.stream(self.stream): + self.user = self.user.npu(non_blocking=True) + self.item = self.item.npu(non_blocking=True) + self.rating = self.rating.npu(non_blocking=True) + + def next(self): + torch.npu.current_stream().wait_stream(self.stream) + user = self.user + item = self.item + rating = self.rating + if user is not None: + self.preload() + return user, item, rating -- Gitee From f57287a62454246f7850212ccfd624263a60f245 Mon Sep 17 00:00:00 2001 From: xingjinliang Date: Thu, 24 Mar 2022 07:02:50 +0000 Subject: [PATCH 2/7] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E4=B8=BAint32=E9=98=B2=E6=AD=A2cast=E7=AE=97?= =?UTF-8?q?=E5=AD=90=E8=B5=B0aicpu?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NeuMF_ID0351_for_PyTorch/src/data.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/PyTorch/dev/cv/image_classification/NeuMF_ID0351_for_PyTorch/src/data.py b/PyTorch/dev/cv/image_classification/NeuMF_ID0351_for_PyTorch/src/data.py index ebdafffd1f..926481c191 100644 --- a/PyTorch/dev/cv/image_classification/NeuMF_ID0351_for_PyTorch/src/data.py +++ b/PyTorch/dev/cv/image_classification/NeuMF_ID0351_for_PyTorch/src/data.py @@ -123,8 +123,11 @@ class SampleGenerator(object): users.append(int(row.userId)) items.append(int(row.negatives[i])) ratings.append(float(0)) # negative samples get 0 rating - dataset = UserItemRatingDataset(user_tensor=torch.LongTensor(users), - item_tensor=torch.LongTensor(items), + # dataset = UserItemRatingDataset(user_tensor=torch.LongTensor(users), + # item_tensor=torch.LongTensor(items), + # target_tensor=torch.FloatTensor(ratings)) + dataset = UserItemRatingDataset(user_tensor=torch.IntTensor(users), + item_tensor=torch.IntTensor(items), target_tensor=torch.FloatTensor(ratings)) return DataLoader(dataset, batch_size=batch_size, shuffle=True, drop_last=True, num_workers=16) -- Gitee From 0f9211e223c4b85c0bdd55badeb7776c28d10018 Mon Sep 17 00:00:00 2001 From: xingjinliang Date: Thu, 24 Mar 2022 07:04:38 +0000 Subject: [PATCH 3/7] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E9=A2=84=E5=A4=84=E7=90=86=E7=AE=97=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NeuMF_ID0351_for_PyTorch/src/engine.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/PyTorch/dev/cv/image_classification/NeuMF_ID0351_for_PyTorch/src/engine.py b/PyTorch/dev/cv/image_classification/NeuMF_ID0351_for_PyTorch/src/engine.py index 41c71feef5..bbcb706d7b 100644 --- a/PyTorch/dev/cv/image_classification/NeuMF_ID0351_for_PyTorch/src/engine.py +++ b/PyTorch/dev/cv/image_classification/NeuMF_ID0351_for_PyTorch/src/engine.py @@ -73,7 +73,7 @@ class Engine(object): def train_single_batch(self, users, items, ratings): assert hasattr(self, 'model'), 'Please specify the exact model !' #if self.config['use_npu'] is True: - users, items, ratings = users.npu(), items.npu(), ratings.npu() + #users, items, ratings = users.npu(), items.npu(), ratings.npu() self.opt.zero_grad() ratings_pred = self.model(users, items) loss = self.crit(ratings_pred.view(-1), ratings) @@ -81,21 +81,28 @@ class Engine(object): with amp.scale_loss(loss, self.opt) as scaled_loss: scaled_loss.backward() self.opt.step() - loss = loss.item() + loss = loss.detach() return loss def train_an_epoch(self, train_loader, epoch_id): assert hasattr(self, 'model'), 'Please specify the exact model !' self.model.train() total_loss = 0 - for batch_id, batch in enumerate(train_loader): + batch_id = 0 + #for batch_id, batch in enumerate(train_loader): + from prefetcher import Prefetcher + prefetcher = Prefetcher(train_loader) + user, item, rating = prefetcher.next() + while user is not None: start_time=time.time() - assert isinstance(batch[0], torch.LongTensor) - user, item, rating = batch[0], batch[1], batch[2] - rating = rating.float() + # assert isinstance(batch[0], torch.LongTensor) + # user, item, rating = batch[0], batch[1], batch[2] + # rating = rating.float() loss = self.train_single_batch(user, item, rating) print('[Training Epoch {}] Batch {}/{}, Loss:{:.3f}, Train-time:{:.4f}'.format(epoch_id, batch_id, len(train_loader), loss,time.time()-start_time)) total_loss += loss + batch_id += 1 + user, item, rating = prefetcher.next() self._writer.add_scalar('model/loss', total_loss, epoch_id) def evaluate(self, evaluate_data, epoch_id): -- Gitee From 20cae0b42fbb8a6d99c5950b9aec659e0b6527a9 Mon Sep 17 00:00:00 2001 From: xingjinliang Date: Thu, 24 Mar 2022 07:06:40 +0000 Subject: [PATCH 4/7] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20PyTo?= =?UTF-8?q?rch/dev/cv/image=5Fclassification/NeuMF=5FID0351=5Ffor=5FPyTorc?= =?UTF-8?q?h/prefetcher.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NeuMF_ID0351_for_PyTorch/prefetcher.py | 67 ------------------- 1 file changed, 67 deletions(-) delete mode 100644 PyTorch/dev/cv/image_classification/NeuMF_ID0351_for_PyTorch/prefetcher.py diff --git a/PyTorch/dev/cv/image_classification/NeuMF_ID0351_for_PyTorch/prefetcher.py b/PyTorch/dev/cv/image_classification/NeuMF_ID0351_for_PyTorch/prefetcher.py deleted file mode 100644 index 5e05ead500..0000000000 --- a/PyTorch/dev/cv/image_classification/NeuMF_ID0351_for_PyTorch/prefetcher.py +++ /dev/null @@ -1,67 +0,0 @@ -# Copyright (c) 2020 Huawei Technologies Co., Ltd -# Copyright (c) 2019, Facebook CORPORATION. -# All rights reserved. -# -# Licensed under the BSD 3-Clause License (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://opensource.org/licenses/BSD-3-Clause -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import torch - - -class Prefetcher(object): - """Prefetcher using on npu device. - - Origin Code URL: - https://github.com/implus/PytorchInsight/blob/master/classification/imagenet_fast.py#L280 - - Args: - loder (torch.utils.data.DataLoader or DataLoader like iterator): - Using to generate inputs after preprocessing. - stream (torch.npu.Stream): Default None. - Because of the limitation of NPU's memory mechanism, - if prefetcher is initialized repeatedly during training, - a defined stream should be introduced to prevent memory leakage; - if prefetcher is initialized only once during training, - a defined stream is not necessary. - - Returns: - float: tensors of shape (k, 5) and (k, 1). Labels are 0-based. - """ - - def __init__(self, loader, stream=None): - self.loader = iter(loader) - self.stream = stream if stream is not None else torch.npu.Stream() - self.preload() - - def preload(self): - try: - self.user, self.item, self.rating = next(self.loader) - assert isinstance(self.user, torch.IntTensor) - self.rating = self.rating.float() - except StopIteration: - self.user = None - self.item = None - return - - with torch.npu.stream(self.stream): - self.user = self.user.npu(non_blocking=True) - self.item = self.item.npu(non_blocking=True) - self.rating = self.rating.npu(non_blocking=True) - - def next(self): - torch.npu.current_stream().wait_stream(self.stream) - user = self.user - item = self.item - rating = self.rating - if user is not None: - self.preload() - return user, item, rating -- Gitee From 1e2bd06210fab4ebaedd0f3af3437386913aaa1e Mon Sep 17 00:00:00 2001 From: xingjinliang Date: Thu, 24 Mar 2022 07:07:01 +0000 Subject: [PATCH 5/7] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E9=A2=84=E5=A4=84=E7=90=86=E7=AE=97=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/prefetcher.py | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 PyTorch/dev/cv/image_classification/NeuMF_ID0351_for_PyTorch/src/prefetcher.py diff --git a/PyTorch/dev/cv/image_classification/NeuMF_ID0351_for_PyTorch/src/prefetcher.py b/PyTorch/dev/cv/image_classification/NeuMF_ID0351_for_PyTorch/src/prefetcher.py new file mode 100644 index 0000000000..5e05ead500 --- /dev/null +++ b/PyTorch/dev/cv/image_classification/NeuMF_ID0351_for_PyTorch/src/prefetcher.py @@ -0,0 +1,67 @@ +# Copyright (c) 2020 Huawei Technologies Co., Ltd +# Copyright (c) 2019, Facebook CORPORATION. +# All rights reserved. +# +# Licensed under the BSD 3-Clause License (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://opensource.org/licenses/BSD-3-Clause +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import torch + + +class Prefetcher(object): + """Prefetcher using on npu device. + + Origin Code URL: + https://github.com/implus/PytorchInsight/blob/master/classification/imagenet_fast.py#L280 + + Args: + loder (torch.utils.data.DataLoader or DataLoader like iterator): + Using to generate inputs after preprocessing. + stream (torch.npu.Stream): Default None. + Because of the limitation of NPU's memory mechanism, + if prefetcher is initialized repeatedly during training, + a defined stream should be introduced to prevent memory leakage; + if prefetcher is initialized only once during training, + a defined stream is not necessary. + + Returns: + float: tensors of shape (k, 5) and (k, 1). Labels are 0-based. + """ + + def __init__(self, loader, stream=None): + self.loader = iter(loader) + self.stream = stream if stream is not None else torch.npu.Stream() + self.preload() + + def preload(self): + try: + self.user, self.item, self.rating = next(self.loader) + assert isinstance(self.user, torch.IntTensor) + self.rating = self.rating.float() + except StopIteration: + self.user = None + self.item = None + return + + with torch.npu.stream(self.stream): + self.user = self.user.npu(non_blocking=True) + self.item = self.item.npu(non_blocking=True) + self.rating = self.rating.npu(non_blocking=True) + + def next(self): + torch.npu.current_stream().wait_stream(self.stream) + user = self.user + item = self.item + rating = self.rating + if user is not None: + self.preload() + return user, item, rating -- Gitee From ae5664a591bace7532a6bf4cc459e26d860c01c8 Mon Sep 17 00:00:00 2001 From: xingjinliang Date: Thu, 24 Mar 2022 07:30:18 +0000 Subject: [PATCH 6/7] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=B3=A8=E9=87=8A?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NeuMF_ID0351_for_PyTorch/src/engine.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/PyTorch/dev/cv/image_classification/NeuMF_ID0351_for_PyTorch/src/engine.py b/PyTorch/dev/cv/image_classification/NeuMF_ID0351_for_PyTorch/src/engine.py index bbcb706d7b..405a722b3e 100644 --- a/PyTorch/dev/cv/image_classification/NeuMF_ID0351_for_PyTorch/src/engine.py +++ b/PyTorch/dev/cv/image_classification/NeuMF_ID0351_for_PyTorch/src/engine.py @@ -72,12 +72,9 @@ class Engine(object): def train_single_batch(self, users, items, ratings): assert hasattr(self, 'model'), 'Please specify the exact model !' - #if self.config['use_npu'] is True: - #users, items, ratings = users.npu(), items.npu(), ratings.npu() self.opt.zero_grad() ratings_pred = self.model(users, items) loss = self.crit(ratings_pred.view(-1), ratings) - #loss.backward() with amp.scale_loss(loss, self.opt) as scaled_loss: scaled_loss.backward() self.opt.step() @@ -89,15 +86,11 @@ class Engine(object): self.model.train() total_loss = 0 batch_id = 0 - #for batch_id, batch in enumerate(train_loader): from prefetcher import Prefetcher prefetcher = Prefetcher(train_loader) user, item, rating = prefetcher.next() while user is not None: start_time=time.time() - # assert isinstance(batch[0], torch.LongTensor) - # user, item, rating = batch[0], batch[1], batch[2] - # rating = rating.float() loss = self.train_single_batch(user, item, rating) print('[Training Epoch {}] Batch {}/{}, Loss:{:.3f}, Train-time:{:.4f}'.format(epoch_id, batch_id, len(train_loader), loss,time.time()-start_time)) total_loss += loss -- Gitee From 9cdf091dc439b427a05b3f066956b2cf93280c76 Mon Sep 17 00:00:00 2001 From: xingjinliang Date: Thu, 24 Mar 2022 07:30:50 +0000 Subject: [PATCH 7/7] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=B3=A8=E9=87=8A?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../image_classification/NeuMF_ID0351_for_PyTorch/src/data.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/PyTorch/dev/cv/image_classification/NeuMF_ID0351_for_PyTorch/src/data.py b/PyTorch/dev/cv/image_classification/NeuMF_ID0351_for_PyTorch/src/data.py index 926481c191..4c7fb24a47 100644 --- a/PyTorch/dev/cv/image_classification/NeuMF_ID0351_for_PyTorch/src/data.py +++ b/PyTorch/dev/cv/image_classification/NeuMF_ID0351_for_PyTorch/src/data.py @@ -123,9 +123,6 @@ class SampleGenerator(object): users.append(int(row.userId)) items.append(int(row.negatives[i])) ratings.append(float(0)) # negative samples get 0 rating - # dataset = UserItemRatingDataset(user_tensor=torch.LongTensor(users), - # item_tensor=torch.LongTensor(items), - # target_tensor=torch.FloatTensor(ratings)) dataset = UserItemRatingDataset(user_tensor=torch.IntTensor(users), item_tensor=torch.IntTensor(items), target_tensor=torch.FloatTensor(ratings)) -- Gitee