public static void VerifyWeightsSum(MemoryAccessor weights0, MemoryAccessor weights1)
        {
            int idx = 0;

            if (weights1 == null)
            {
                if (weights0 == null)
                {
                    return;
                }

                foreach (var item in weights0.GetItemsAsRawBytes())
                {
                    if (!_CheckWeightSum(item, weights0.Attribute.Encoding))
                    {
                        throw new ArgumentException($"Weight Sum invalid at Index {idx}", nameof(weights0));
                    }

                    ++idx;
                }

                return;
            }

            // check for WEIGHTS_0 + WEIGHTS_1

            if (weights0 == null)
            {
                throw new ArgumentNullException(nameof(weights0));
            }

            if (weights0.Attribute.Encoding != weights1.Attribute.Encoding)
            {
                throw new ArgumentException("WEIGHTS_0 and WEIGHTS_1 format mismatch.", nameof(weights1.Attribute));
            }

            var         len = weights0.Attribute.ItemByteLength;
            Span <Byte> dst = stackalloc byte[len * 2];

            var zip = weights0.GetItemsAsRawBytes()
                      .Zip(weights1.GetItemsAsRawBytes(), (a, b) => (a, b));

            foreach (var(a, b) in zip)
            {
                a.AsSpan().CopyTo(dst);
                b.AsSpan().CopyTo(dst.Slice(len));

                if (!_CheckWeightSum(dst, weights0.Attribute.Encoding))
                {
                    throw new ArgumentException($"Weight Sum invalid at Index {idx}", nameof(weights1));
                }

                ++idx;
            }
        }
        public static void ValidateWeightsSum(Validation.ValidationContext result, MemoryAccessor weights0, MemoryAccessor weights1)
        {
            int idx = 0;

            if (weights1 == null)
            {
                if (weights0 == null)
                {
                    return;
                }

                foreach (var item in weights0.GetItemsAsRawBytes())
                {
                    if (!_CheckWeightSum(item, weights0.Attribute.Encoding))
                    {
                        result.AddDataError($"Weight Sum invalid at Index {idx}");
                    }

                    ++idx;
                }

                return;
            }

            if (weights0 == null)
            {
                result.AddLinkError("");
                return;
            }

            var         len = weights0.Attribute.ItemByteLength;
            Span <Byte> dst = stackalloc byte[len * 2];

            var zip = weights0.GetItemsAsRawBytes().Zip(weights1.GetItemsAsRawBytes(), (a, b) => (a, b));

            foreach (var(a, b) in zip)
            {
                a.AsSpan().CopyTo(dst);
                b.AsSpan().CopyTo(dst.Slice(len));

                if (!_CheckWeightSum(dst, weights0.Attribute.Encoding))
                {
                    result.AddDataError($"Weight Sum invalid at Index {idx}");
                }

                ++idx;
            }
        }
Example #3
0
        public static void SanitizeWeightsSum(MemoryAccessor weights0, MemoryAccessor weights1)
        {
            if (weights1 == null)
            {
                if (weights0 == null)
                {
                    return;
                }

                foreach (var item in weights0.GetItemsAsRawBytes())
                {
                    _SanitizeWeightSum(item, weights0.Attribute.Encoding);
                }

                return;
            }

            if (weights0 == null)
            {
                return;
            }

            var         len = weights0.Attribute.ItemByteLength;
            Span <Byte> dst = stackalloc byte[len * 2];

            var zip = weights0.GetItemsAsRawBytes().Zip(weights1.GetItemsAsRawBytes(), (a, b) => (a, b));

            foreach (var(a, b) in zip)
            {
                a.AsSpan().CopyTo(dst);
                b.AsSpan().CopyTo(dst.Slice(len));

                if (_SanitizeWeightSum(dst, weights0.Attribute.Encoding))
                {
                    dst.Slice(0, len).CopyTo(a);
                    dst.Slice(len, len).CopyTo(b);
                }
            }
        }