Feature/lod tensor array (#5379)
* Use stable_sort in lod_rank_table It is easy to debug and test when use `stable_sort`and the time complexity is not changed. * Add LoDTensorArraymobile_baidu
parent
cb0118f3e5
commit
2be4c3cb62
@ -0,0 +1,23 @@
|
||||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
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. */
|
||||
|
||||
#pragma once
|
||||
#include <vector>
|
||||
#include "paddle/framework/lod_tensor.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace framework {
|
||||
using LoDTensorArray = std::vector<LoDTensor>;
|
||||
}
|
||||
} // namespace paddle
|
||||
@ -0,0 +1,38 @@
|
||||
import unittest
|
||||
import paddle.v2.framework.core as core
|
||||
import numpy
|
||||
|
||||
|
||||
class TestLoDTensorArray(unittest.TestCase):
|
||||
def test_get_set(self):
|
||||
scope = core.Scope()
|
||||
arr = scope.var('tmp_lod_tensor_array')
|
||||
tensor_array = arr.get_lod_tensor_array()
|
||||
self.assertEqual(0, len(tensor_array))
|
||||
cpu = core.CPUPlace()
|
||||
for i in xrange(10):
|
||||
t = core.LoDTensor()
|
||||
t.set(numpy.array([i], dtype='float32'), cpu)
|
||||
t.set_lod([[0, 1]])
|
||||
tensor_array.append(t)
|
||||
|
||||
self.assertEqual(10, len(tensor_array))
|
||||
|
||||
for i in xrange(10):
|
||||
t = tensor_array[i]
|
||||
self.assertEqual(numpy.array(t), numpy.array([i], dtype='float32'))
|
||||
self.assertEqual([[0, 1]], t.lod())
|
||||
|
||||
t = core.LoDTensor()
|
||||
t.set(numpy.array([i + 10], dtype='float32'), cpu)
|
||||
t.set_lod([[0, 2]])
|
||||
tensor_array[i] = t
|
||||
t = tensor_array[i]
|
||||
self.assertEqual(
|
||||
numpy.array(t), numpy.array(
|
||||
[i + 10], dtype='float32'))
|
||||
self.assertEqual([[0, 2]], t.lod())
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Loading…
Reference in new issue