Inheritance: GoodAI.Core.Memory.MemBlockAttribute
        public void CanBeComputedEvaluatesToTrue()
        {
            var dims = new TensorDimensions(3, 4, -1, 5) { Size = 3*4*5*13 };

            Assert.True(dims.CanBeComputed);
            Assert.Equal(13, dims[2]);  // also check that the free dimension was correctly computed
        }
        public void ComputedDimCanBeOne()
        {
            var dims = new TensorDimensions(-1, 10) { Size = 10 };

            Assert.True(dims.CanBeComputed);
            Assert.Equal(1, dims[0]);
        }
Esempio n. 3
0
        /// <summary>
        /// In case of error, returns the originalDimensions and a non-empty error message in the output argument.
        /// </summary>
        public TensorDimensions TryToApply(TensorDimensions originalDimensions, out string errorMessage)
        {
            var dimsOrError = ComputeDimensions(originalDimensions.ElementCount);

            errorMessage = dimsOrError.Item2?.Message ?? string.Empty;

            return(dimsOrError.Item1 ?? originalDimensions);
        }
Esempio n. 4
0
        public bool TryToApply(TensorDimensions originalDims, out TensorDimensions customDims)
        {
            TensorDimensions adjustedDims = TryComputeDimensions(originalDims.ElementCount);

            customDims = adjustedDims ?? originalDims;

            return(adjustedDims != null);
        }
        public void ApplyFallbackTheory(CustomDimensionsHint hint, TensorDimensions originalDims)
        {
            TensorDimensions resultDims;

            bool didApply = hint.TryToApply(originalDims, out resultDims);

            Assert.Equal(originalDims, resultDims);
            Assert.False(didApply);
        }
        public void ConstructsWithVariableNumberOfParams()
        {
            var dims = new TensorDimensions(2, 3);

            Assert.Equal(2, dims.Count);
            Assert.Equal(2, dims[0]);
            Assert.Equal(3, dims[1]);
            Assert.False(dims.IsCustom);  // dimensions created in code are "default" and should not be saved to project
        }
 public CustomDimsTestCase(TensorDimensions dims, CustomDimensionsHint customDims,
     RenderingMethod method, int vectorElements, Size expectedSize)
 {
     Dims = dims;
     CustomDims = customDims;
     Method = method;
     VectorElements = vectorElements;
     ExpectedSize = expectedSize;
 }
        public void AnyDimensionCanBeZero()
        {
            var rank1Dims = new TensorDimensions(0);
            Assert.Equal(0, rank1Dims.ElementCount);

            var rankNDims = new TensorDimensions(3, 0, 5);
            Assert.Equal(3, rankNDims[0]);
            Assert.Equal(0, rankNDims.ElementCount);
        }
Esempio n. 9
0
        public TensorDimensions Apply(TensorDimensions originalDims)
        {
            var dimsOrError = ComputeDimensions(originalDims.ElementCount);

            if (dimsOrError.Item1 == null)
            {
                throw dimsOrError.Item2;
            }

            return(dimsOrError.Item1);
        }
        public void PrintIndicatesMismatchedDimsAndSize()
        {
            var dims = new TensorDimensions(3, 3) { Size = 4 };

            Assert.Equal("3×3 (!)", dims.Print());
        }
        public void ParseKeepsDimensionsOfSizeOne()
        {
            var dims = new TensorDimensions();

            dims.Parse("1, 5, *, 1, 1");

            Assert.Equal(5, dims.Count);
            Assert.Equal(1, dims[0]);
            Assert.Equal(5, dims[1]);
            Assert.Equal(1, dims[4]);
            Assert.Equal("", dims.LastSetWarning);
        }
        public void ParseDoesNotAutoAddDimWhenSizeMatches()
        {
            var dims = new TensorDimensions() { Size = 2*2*2 };
            dims.Parse("2, 2, 2");

            Assert.Equal(3, dims.Count);
            Assert.Equal(2, dims[0]);
            Assert.Equal(2, dims[1]);
        }
        public void HashCodeIsCachedProperly()
        {
            var dims = new TensorDimensions(3, 5, 7);

            int hashCode = dims.GetHashCode();
            Assert.Equal(hashCode, dims.GetHashCode());
        }
        private static MyMemoryBlock<float> GetMemBlockWithCustomDims(string dimensionsSource)
        {
            var customDims = new TensorDimensions();
            customDims.Parse(dimensionsSource);

            return new MyMemoryBlock<float> { Dims = customDims };
        }
        public void PrintsOneOne()
        {
            var dims = new TensorDimensions(1, 1);

            Assert.Equal("1 (!)", dims.Print(hideTrailingOnes: true));
        }
Esempio n. 16
0
 public bool Equals(TensorDimensions dimensionsHint)
 {
     return base.Equals(dimensionsHint);
 }
 public ColumnHintTestData(TensorDimensions initialDims, int columnHint, TensorDimensions expectedDims, string comment)
 {
     InitialDims = initialDims;
     ColumnHint = columnHint;
     ExpectedDims = expectedDims;
     Comment = comment;
 }
 private static MyAbstractMemoryBlock GetMemBlock(TensorDimensions dims)
 {
     return new MyMemoryBlock<float> { Dims = dims };
 }
 public void TranspositionTests(TensorDimensions initial, TensorDimensions expected)
 {
     Assert.True(initial.Transpose().Equals(expected));
 }
        public void PrintsEmptyDims()
        {
            var dims = new TensorDimensions();

            Assert.Equal("0", dims.Print());
        }
        public void PrintsComputedTrailingOne()
        {
            var dims = new TensorDimensions(4, 2, -1) { Size = 8 };

            Assert.Equal("4×2×1", dims.Print(hideTrailingOnes: true));
        }
        public void DimensionsOfSizeOneAreAllowed()
        {
            var dims = new TensorDimensions();

            dims.Set(new []{ 5, 1, 1 });
        }
        public void PrintsLeadingOrMiddleOnes()
        {
            var dims = new TensorDimensions(1, 1, -1, 5, 1, 2, 1);

            Assert.Equal("1×1×?×5×1×2", dims.Print(hideTrailingOnes: true));
        }
        public void EmptyDimensionsCanBeComputed()
        {
            var dims = new TensorDimensions();
            Assert.False(dims.CanBeComputed);

            dims.Size = 4;
            Assert.True(dims.CanBeComputed);
            Assert.Equal(4, dims[0]);
        }
        public void CanBeComputedEvaluatesToFalse()
        {
            var dims = new TensorDimensions(3, 4, -1, 5) { Size = 37 };

            Assert.False(dims.CanBeComputed);
        }
        public bool TryToApply(TensorDimensions originalDims, out TensorDimensions customDims)
        {
            TensorDimensions adjustedDims = ComputeDimensions(originalDims.ElementCount);

            customDims = adjustedDims ?? originalDims;

            return (adjustedDims != null);
        }
Esempio n. 27
0
 public TensorDimensions TryToApply(TensorDimensions originalDims)
 {
     return(TryComputeDimensions(originalDims.ElementCount) ?? originalDims);
 }
        public void ParseAutoAddsLeadingDim()
        {
            var dims = new TensorDimensions();
            dims.Parse("2, 2, 2");

            Assert.Equal(4, dims.Count);
            Assert.Equal(-1, dims[0]);
            Assert.Equal(2, dims[1]);
        }
        private Size ComputeTiledTextureSize(TensorDimensions dims, MyAbstractMemoryBlock target)
        {
            int effectivePixelsDisplayed;

            if (Method == RenderingMethod.RGB)
            {
                // check if:
                // memory block has 3 chanels
                // chanels are divisible by requested TileWidth and TileHeight
                // TileWidth and TileHeight are not too big
                if (
                    dims.ElementCount % 3 != 0 ||
                    dims.ElementCount / TileWidth / TileHeight % 3 != 0 ||
                    dims.ElementCount / TileWidth / TileHeight / 3 == 0)
                {
                    MyLog.WARNING.WriteLine("Memory block '{0}: {1}' observer: {2}", Target.Owner.Name, Target.Name,
                        "RGB rendering, but Memory block.count and TileWidth and TileHeight values incompatible with the RGB format!" +
                        " Swtiching to RedGreenScale. Use correct CustomDimensions");
                    Method = RenderingMethod.RedGreenScale;
                    effectivePixelsDisplayed = dims.ElementCount;
                }
                else
                {
                    effectivePixelsDisplayed = (int)Math.Ceiling((decimal)((float)dims.ElementCount / (float)3));
                }
            }
            else
            {
                effectivePixelsDisplayed = dims.ElementCount;
            }

            if (TileWidth * TileHeight * TilesInRow > effectivePixelsDisplayed)
            {
                TilesInRow = effectivePixelsDisplayed / (TileWidth * TileHeight);
                String message = " (Parsed from " + (UseCustomDimensions ? "CustomDimensions)" : "MemoryBlock.Dims)");
                MyLog.WARNING.WriteLine("Memory block '{0}: {1}' observer: {2}", Target.Owner.Name, Target.Name,
                    "TilesInRow too big, adjusting to the max. value " + TilesInRow + "\n\t\t..for TileWidth=" + TileWidth + ", TileHeight=" + TileHeight + message);
            }

            m_noBlocks = effectivePixelsDisplayed / (TileWidth * TileHeight);
            TilesInColumn = (int)Math.Ceiling((decimal)((float)m_noBlocks / (float)TilesInRow));

            return new Size(TileWidth * TilesInRow, TilesInColumn * TileHeight);
        }
        public void ElementCountIsCachedProperly()
        {
            var dims = new TensorDimensions(3, 5, 7);

            Assert.Equal(3*5*7, dims.ElementCount);
            Assert.Equal(3*5*7, dims.ElementCount);
        }
        public void DoesNotPrintTrailingOnes()
        {
            var dims = new TensorDimensions(5, 1, 1) { Size = 5 };

            Assert.Equal("5", dims.Print(hideTrailingOnes: true));
        }
        // TODO(Premek): Report warnings using a logger interface.
        internal static Size ComputeCustomTextureSize(TensorDimensions dims, CustomDimensionsHint customDims,
            RenderingMethod method, int vectorElements, out string warning)
        {
            warning = "";

            if (dims.IsEmpty)
                return Size.Empty;

            bool isDivisible;
            string divisorName = (method == RenderingMethod.RGB) ? "3 (RGB channel count)" : "vector element count";

            bool isRowVector = (dims.Rank == 1) || (dims.Rank == 2 && dims[1] == 1);
            bool isColumnVector = !isRowVector && (dims.Rank == 2) && (dims[0] == 1);

            TensorDimensions adjustedDims;
            bool didApplyCustomDims = customDims.TryToApply(dims, out adjustedDims);
            if (!customDims.IsEmpty && !didApplyCustomDims)
                warning = "Could not apply custom dimensions (the element count must match the original).";

            if (!didApplyCustomDims && (isRowVector || isColumnVector))
            {
                return ComputeTextureSizeForVector(dims.ElementCount, isRowVector, method, vectorElements, divisorName,
                    ref warning);
            }

            int shrinkedLastDim = ShrinkSizeForRenderingMethod(adjustedDims[adjustedDims.Rank - 1], method,
                vectorElements, out isDivisible);

            if (!isDivisible || (shrinkedLastDim == 0))
            {
                if (string.IsNullOrEmpty(warning))
                    warning = string.Format("The last dimension is {0} {1}. Ignoring dimensions.",
                        (!isDivisible) ? "not divisible by" : "smaller than", divisorName);

                return ComputeTextureSize(
                    ShrinkSizeForRenderingMethod(dims.ElementCount, method, vectorElements, out isDivisible));
            }

            // Squash all dimensions except the first one together.
            // TODO(Premek): Decide according to actual sizes of the dimensions.
            int squashedOtherDims = shrinkedLastDim;
            for (int i = 1; i < adjustedDims.Rank - 1; i++)
                squashedOtherDims *= adjustedDims[i];

            return new Size(adjustedDims[0], squashedOtherDims);
        }
Esempio n. 33
0
 public bool Equals(TensorDimensions dimensionsHint)
 {
     return(base.Equals(dimensionsHint));
 }
 public TensorDimensions TryToApply(TensorDimensions originalDims)
 {
     return ComputeDimensions(originalDims.ElementCount) ?? originalDims;
 }