예제 #1
0
    public void TestAtomicAdd()
    {
        int batchSize = 5;
        int vocabSize = 20;

        TensorAllocator.InitDevices(ProcessorTypeEnums.CPU, new int[] { 0 });
        var graph = new ComputeGraphTensor(new WeightTensorFactory(), 0, true);

        var tensorA = BuildRandomTensor(shape: new long[2] {
            batchSize, vocabSize
        }, name: "tensorA", isTrainable: true);
        var   tensorAWeights = tensorA.ToWeightArray();
        float sumA           = tensorAWeights.Sum();

        var tensorB = BuildRandomTensor(shape: new long[2] {
            batchSize, vocabSize
        }, name: "tensorB", isTrainable: true);
        var   tensorBWeights = tensorB.ToWeightArray();
        float sumB           = tensorBWeights.Sum();

        float sum = sumA + sumB;

        Ops.AtomicAdd(tensorA.TWeight, tensorB.TWeight);
        var   tensorSumWeights = tensorA.ToWeightArray();
        float sum2             = tensorSumWeights.Sum();

        Assert.IsTrue(Math.Round(sum, 5) == Math.Round(sum2, 5));
    }