Exemplo n.º 1
0
        private static void EmitFmul(
            EmitterContext context,
            Instruction fpType,
            MultiplyScale scale,
            Operand srcA,
            Operand srcB,
            int rd,
            bool negateB,
            bool saturate,
            bool writeCC)
        {
            bool isFP64 = fpType == Instruction.FP64;

            srcB = context.FPNegate(srcB, negateB, fpType);

            if (scale != MultiplyScale.NoScale)
            {
                Operand scaleConst = scale switch
                {
                    MultiplyScale.D2 => ConstF(0.5f),
                    MultiplyScale.D4 => ConstF(0.25f),
                    MultiplyScale.D8 => ConstF(0.125f),
                    MultiplyScale.M2 => ConstF(2f),
                    MultiplyScale.M4 => ConstF(4f),
                    MultiplyScale.M8 => ConstF(8f),
                    _ => ConstF(1f) // Invalid, behave as if it had no scale.
                };

                if (scaleConst.AsFloat() == 1f)
                {
                    context.Config.GpuAccessor.Log($"Invalid FP multiply scale \"{scale}\".");
                }

                if (isFP64)
                {
                    scaleConst = context.FP32ConvertToFP64(scaleConst);
                }

                srcA = context.FPMultiply(srcA, scaleConst, fpType);
            }

            Operand res = context.FPSaturate(context.FPMultiply(srcA, srcB, fpType), saturate, fpType);

            SetDest(context, res, rd, isFP64);

            SetFPZnFlags(context, res, writeCC, fpType);
        }
Exemplo n.º 2
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>   Sets the parameters. </summary>
        ///
        /// <param name="func">         The function. </param>
        /// <param name="modelData">    Information describing the model. </param>
        ////////////////////////////////////////////////////////////////////////////////////////////////////

        static void SetParams(Function func, NpzDictionary modelData)
        {
            if (func is Linear)
            {
                Linear linear = (Linear)func;

                Array.Copy(Real.GetArray(modelData[func.Name + "/W.npy"]), linear.Weight.Data, linear.Weight.Data.Length);

                if (!linear.NoBias)
                {
                    Array.Copy(Real.GetArray(modelData[func.Name + "/b.npy"]), linear.Bias.Data, linear.Bias.Data.Length);
                }
            }
            else if (func is Convolution2D)
            {
                Convolution2D conv2D = (Convolution2D)func;

                Array.Copy(Real.GetArray(modelData[func.Name + "/W.npy"]), conv2D.Weight.Data, conv2D.Weight.Data.Length);

                if (!conv2D.NoBias)
                {
                    Array.Copy(Real.GetArray(modelData[func.Name + "/b.npy"]), conv2D.Bias.Data, conv2D.Bias.Data.Length);
                }
            }
            else if (func is Deconvolution2D)
            {
                Deconvolution2D deconv2D = (Deconvolution2D)func;

                Array.Copy(Real.GetArray(modelData[func.Name + "/W.npy"]), deconv2D.Weight.Data, deconv2D.Weight.Data.Length);

                if (!deconv2D.NoBias)
                {
                    Array.Copy(Real.GetArray(modelData[func.Name + "/b.npy"]), deconv2D.Bias.Data, deconv2D.Bias.Data.Length);
                }
            }
            else if (func is EmbedID)
            {
                EmbedID embed = (EmbedID)func;

                Array.Copy(Real.GetArray(modelData[func.Name + "/W.npy"]), embed.Weight.Data, embed.Weight.Data.Length);
            }
            else if (func is BatchNormalization)
            {
                BatchNormalization bn = (BatchNormalization)func;

                Array.Copy(Real.GetArray(modelData[func.Name + "/beta.npy"]), bn.Beta.Data, bn.Beta.Data.Length);
                Array.Copy(Real.GetArray(modelData[func.Name + "/gamma.npy"]), bn.Gamma.Data, bn.Gamma.Data.Length);

                if (bn.IsTrain)
                {
                    if (modelData.ContainsKey(func.Name + "/avg_mean.npy"))
                    {
                        Array.Copy(Real.GetArray(modelData[func.Name + "/avg_mean.npy"]), bn.AvgMean.Data, bn.AvgMean.Data.Length);
                    }
                    if (modelData.ContainsKey(func.Name + "/avg_var.npy"))
                    {
                        Array.Copy(Real.GetArray(modelData[func.Name + "/avg_var.npy"]), bn.AvgVar.Data, bn.AvgVar.Data.Length);
                    }
                }
            }
            else if (func is MultiplyScale)
            {
                MultiplyScale scale = (MultiplyScale)func;

                Array.Copy(Real.GetArray(modelData[func.Name + "/W.npy"]), scale.Weight.Data, scale.Weight.Data.Length);

                if (scale.BiasTerm)
                {
                    Array.Copy(Real.GetArray(modelData[func.Name + "/bias/b.npy"]), scale.Bias.Data, scale.Bias.Data.Length);
                }
            }
        }
Exemplo n.º 3
0
        static void SetParams <T>(Function <T> func, NpzDictionary modelData) where T : unmanaged, IComparable <T>
        {
            if (func is Linear <T> )
            {
                Linear <T> linear = (Linear <T>)func;

                linear.Weight.Data = modelData[func.Name + "/W.npy"].FlattenEx <T>();

                if (linear.Bias != null)
                {
                    linear.Bias.Data = modelData[func.Name + "/b.npy"].FlattenEx <T>();
                }
            }
            else if (func is Convolution2D <T> )
            {
                Convolution2D <T> conv2D = (Convolution2D <T>)func;

                conv2D.Weight.Data = modelData[func.Name + "/W.npy"].FlattenEx <T>();

                if (conv2D.Bias != null)
                {
                    conv2D.Bias.Data = modelData[func.Name + "/b.npy"].FlattenEx <T>();
                }
            }
            else if (func is Deconvolution2D <T> )
            {
                Deconvolution2D <T> deconv2D = (Deconvolution2D <T>)func;

                deconv2D.Weight.Data = modelData[func.Name + "/W.npy"].FlattenEx <T>();

                if (deconv2D.Bias != null)
                {
                    deconv2D.Bias.Data = modelData[func.Name + "/b.npy"].FlattenEx <T>();
                }
            }
            else if (func is EmbedID <T> )
            {
                EmbedID <T> embed = (EmbedID <T>)func;
                embed.Weight.Data = modelData[func.Name + "/W.npy"].FlattenEx <T>();
            }
            else if (func is BatchNormalization <T> )
            {
                BatchNormalization <T> bn = (BatchNormalization <T>)func;

                bn.Beta.Data  = modelData[func.Name + "/beta.npy"].FlattenEx <T>();
                bn.Gamma.Data = modelData[func.Name + "/gamma.npy"].FlattenEx <T>();

                if (bn.Train)
                {
                    if (modelData.ContainsKey(func.Name + "/avg_mean.npy"))
                    {
                        bn.AvgMean.Data = modelData[func.Name + "/avg_mean.npy"].FlattenEx <T>();
                    }
                    if (modelData.ContainsKey(func.Name + "/avg_var.npy"))
                    {
                        bn.AvgVar.Data = modelData[func.Name + "/avg_var.npy"].FlattenEx <T>();
                    }
                }
            }
            else if (func is MultiplyScale <T> )
            {
                MultiplyScale <T> scale = (MultiplyScale <T>)func;

                scale.Weight.Data = modelData[func.Name + "/W.npy"].FlattenEx <T>();

                if (scale.BiasTerm)
                {
                    scale.Bias.Data = modelData[func.Name + "/bias/b.npy"].FlattenEx <T>();
                }
            }
            else if (func is LSTM <T> )
            {
                LSTM <T> lstm = (LSTM <T>)func;

                lstm.lateral.Weight.Data = modelData[func.Name + "/lateral/W.npy"].FlattenEx <T>();
                lstm.upward.Weight.Data  = modelData[func.Name + "/upward/W.npy"].FlattenEx <T>();
                lstm.upward.Bias.Data    = modelData[func.Name + "/upward/b.npy"].FlattenEx <T>();
            }
        }