• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2021 Huawei Technologies Co., Ltd
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ============================================================================
15
16import numpy as np
17import pytest
18
19import mindspore.context as context
20import mindspore.nn as nn
21from mindspore import Tensor
22from mindspore.common.api import ms_function
23from mindspore.common.parameter import Parameter
24from mindspore.ops import operations as P
25import mindspore as ms
26
27
28def create_tensor(capcity, shapes, dtypes):
29    buffer = []
30    for i in range(len(shapes)):
31        buffer.append(Tensor(np.zeros(((capcity,)+shapes[i])), dtypes[i]))
32    return buffer
33
34
35class RLBuffer(nn.Cell):
36    def __init__(self, batch_size, capcity, shapes, types):
37        super(RLBuffer, self).__init__()
38        self.buffer = create_tensor(capcity, shapes, types)
39        self._capacity = capcity
40        self.count = Parameter(Tensor(0, ms.int32), name="count")
41        self.head = Parameter(Tensor(0, ms.int32), name="head")
42        self.buffer_append = P.BufferAppend(self._capacity, shapes, types)
43        self.buffer_get = P.BufferGetItem(self._capacity, shapes, types)
44        self.buffer_sample = P.BufferSample(
45            self._capacity, batch_size, shapes, types)
46        self.randperm = P.Randperm(max_length=capcity, pad=-1)
47        self.reshape = P.Reshape()
48
49    @ms_function
50    def append(self, exps):
51        return self.buffer_append(self.buffer, exps, self.count, self.head)
52
53    @ms_function
54    def get(self, index):
55        return self.buffer_get(self.buffer, self.count, self.head, index)
56
57    @ms_function
58    def sample(self):
59        return self.buffer_sample(self.buffer, self.count, self.head)
60
61
62s = Tensor(np.array([2, 2, 2, 2]), ms.float32)
63a = Tensor(np.array([0, 1]), ms.int32)
64r = Tensor(np.array([1]), ms.float32)
65s_ = Tensor(np.array([3, 3, 3, 3]), ms.float32)
66exp = [s, a, r, s_]
67exp1 = [s_, a, r, s]
68
69
70@ pytest.mark.level0
71@ pytest.mark.platform_x86_gpu_training
72@ pytest.mark.env_onecard
73def test_Buffer():
74    context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
75    buffer = RLBuffer(batch_size=32, capcity=100, shapes=[(4,), (2,), (1,), (4,)], types=[
76        ms.float32, ms.int32, ms.float32, ms.float32])
77    print("init buffer:\n", buffer.buffer)
78    for _ in range(0, 110):
79        buffer.append(exp)
80    buffer.append(exp1)
81    print("buffer append:\n", buffer.buffer)
82    b = buffer.get(-1)
83    print("buffer get:\n", b)
84    bs = buffer.sample()
85    print("buffer sample:\n", bs)
86