public void Dispose() { m_Process = null; m_Allocator = null; GC.SuppressFinalize(this); }
public Tensor(IAllocator allocator, DType elementType, long[] sizes, long[] strides) { this.sizes = sizes; this.strides = strides; this.storageOffset = 0; this.storage = allocator.Allocate(elementType, TensorDimensionHelpers.GetStorageSize(sizes, strides)); }
public WeightTensor(int rows, int columns, int deviceId, bool normal = false) { DeviceId = deviceId; allocator = TensorAllocator.Allocator(DeviceId); Rows = rows; Columns = columns; var n = rows * columns; float[] weight = new float[n]; var scale = (float)Math.Sqrt(1.0 / (rows * columns)); if (normal) { scale = 0.08f; } for (int i = 0; i < n; i++) { weight[i] = RandomGenerator.NormalRandom(0.0f, scale); } TGradient = new Tensor(allocator, DType.Float32, Rows, Columns); Ops.Fill(TGradient, 0.0f); TWeight = Tensor.FromArray(allocator, weight).View(Rows, Columns); }
public Conv2Layer(IAllocator allocator, SeedSource seedSource, DType elementType, int batchSize, int inputWidth, int inputHeight, int nInputPlane, int nOutputPlane, ConvolutionDesc2d cd) { this.cd = cd; this.weight = new NDArray(allocator, elementType, nOutputPlane, nInputPlane * cd.kW * cd.kH); this.bias = new NDArray(allocator, elementType, nOutputPlane, 1); this.gradWeight = new NDArray(allocator, elementType, this.weight.Shape); this.gradBias = new NDArray(allocator, elementType, this.bias.Shape); inputSizes = new long[] { batchSize, nInputPlane, inputHeight, inputWidth }; this.gradInput = new NDArray(allocator, elementType, inputSizes); outputSizes = SpatialConvolutionMM.OutputSize(inputSizes, weight.Shape, cd); this.activation = new NDArray(allocator, elementType, outputSizes); this.OutputSizes = outputSizes; var stdv = 1.0f / (float)Math.Sqrt(cd.kW * cd.kH * nInputPlane); Ops.RandomUniform(weight, seedSource, -stdv, stdv); Ops.RandomUniform(bias, seedSource, -stdv, stdv); }
public ClassNLLCriterion(IAllocator allocator, int batchSize, int nClasses) { this.allocator = allocator; this.output = new NDArray(allocator, DType.Float32, 1); this.gradInput = new NDArray(allocator, DType.Float32, batchSize, nClasses); }
public static Tensor Constant(float value, IAllocator allocator, DType dtype, params long[] sizes) { Tensor tensor = new Tensor(allocator, dtype, sizes); TOps.Fill(tensor, value); return(tensor); }
public WeightTensor(long[] sizes, int deviceId, string name = "", bool isTrainable = false, bool normal = false) { Name = name; DeviceId = deviceId; IsTrainable = isTrainable; allocator = TensorAllocator.Allocator(DeviceId); Sizes = sizes; if (normal) { var n = Rows * Columns; float[] weight = new float[n]; var scale = (float)Math.Sqrt(1.0 / (Rows * Columns)); if (normal) { scale = 0.08f; } for (int i = 0; i < n; i++) { weight[i] = RandomGenerator.NormalRandom(0.0f, scale); } TGradient = new Tensor(allocator, DType.Float32, Sizes); Ops.Fill(TGradient, 0.0f); TWeight = Tensor.FromArray(allocator, weight).View(Sizes); } }
private void Init(PythonContext inPython, string stubPath, IAllocator inAllocator) { this.GIL = new Lock(); this.python = inPython; this.allocator = inAllocator; this.importNames.Push(""); this.importFiles.Push(null); this.CreateScratchModule(); this.kindaDictProxy = this.CreateFromSnippet(CodeSnippets.KINDA_DICT_PROXY_CODE, "KindaDictProxy"); if (stubPath != null) { this.stub = new StubReference(stubPath); this.stub.Init(new dgt_getfuncptr(this.GetFuncPtr), new dgt_registerdata(this.RegisterData)); string path = Environment.GetEnvironmentVariable("PATH"); string newpath = path + ";" + Path.Combine(Path.GetDirectoryName(stubPath), "support"); Environment.SetEnvironmentVariable("PATH", newpath); this.ReadyBuiltinTypes(); this.importer = new PydImporter(); this.removeSysHacks = this.CreateFromSnippet(CodeSnippets.INSTALL_IMPORT_HOOK_CODE, "remove_sys_hacks"); // TODO: load builtin modules only on demand? this.stub.LoadBuiltinModule("posix"); this.stub.LoadBuiltinModule("mmap"); this.stub.LoadBuiltinModule("_csv"); } this.alive = true; }
public Conv2Cudnn(IAllocator allocator, SeedSource seedSource, DType elementType, int batchSize, int inputWidth, int inputHeight, int nInputPlane, int nOutputPlane, ConvolutionDesc2d cd) : base(allocator, seedSource, elementType, batchSize, inputWidth, inputHeight, nInputPlane, nOutputPlane, cd) { // Reshape weight and bias - CuDNN expects the dimensions to be structured slightly differently this.weight = ViewReplace(this.weight, nOutputPlane, nInputPlane, cd.kH, cd.kW); this.bias = ViewReplace(this.bias, 1, nOutputPlane, 1, 1); this.gradWeight = ViewReplace(this.gradWeight, this.weight.Shape); this.gradBias = ViewReplace(this.gradBias, this.bias.Shape); var fwdWorkspace = DNN.GetConvolutionForwardWorkspaceSize(allocator, fwdAlgo, cd, new TensorShape(elementType, new long[] { batchSize, nInputPlane, inputHeight, inputWidth }), new TensorShape(weight), new TensorShape(activation)); var bwdFilterWorkspace = DNN.GetConvolutionBackwardFilterWorkspaceSize(allocator, bwdFilterAlgo, cd, new TensorShape(elementType, new long[] { batchSize, nInputPlane, inputHeight, inputWidth }), new TensorShape(activation), new TensorShape(weight)); var bwdFilterInputWorkspace = DNN.GetConvolutionBackwardDataWorkspaceSize(allocator, bwdDataAlgo, cd, new TensorShape(weight), new TensorShape(activation), new TensorShape(elementType, new long[] { batchSize, nInputPlane, inputHeight, inputWidth })); var workspaceSize = Math.Max(Math.Max(fwdWorkspace, bwdFilterWorkspace), bwdFilterInputWorkspace); this.workspace = (CudaStorage)allocator.Allocate(DType.UInt8, workspaceSize); }
public static DataSet BuildSet(IAllocator allocator, DigitImage[] images) { var inputs = new NDArray(allocator, DType.Float32, images.Length, MnistParser.ImageSize, MnistParser.ImageSize); var outputs = new NDArray(allocator, DType.Float32, images.Length, MnistParser.LabelCount); var cpuAllocator = new TensorSharp.Cpu.CpuAllocator(); for (int i = 0; i < images.Length; ++i) { var target = inputs.TVar().Select(0, i); Variable.FromArray(images[i].pixels, cpuAllocator) .AsType(DType.Float32) .ToDevice(allocator) .Evaluate(target); target.Div(255) .Evaluate(target); } Ops.FillOneHot(outputs, MnistParser.LabelCount, images.Select(x => (int)x.label).ToArray()); var targetValues = NDArray.FromArray(allocator, images.Select(x => (float)x.label).ToArray()); return(new DataSet() { inputs = inputs, targets = outputs, targetValues = targetValues }); }
public static NDArray Constant(float value, IAllocator allocator, DType dtype, params long[] sizes) { NDArray tensor = new NDArray(allocator, dtype, sizes); TOps.Fill(tensor, value); return(tensor); }
public static long GetConvolutionBackwardDataWorkspaceSize(IAllocator allocator, DNNConvolutionBwdDataAlgo algo, Cpu.ConvolutionDesc2d cd, TensorShape w, TensorShape dy, TensorShape dx) { if (!(allocator is CudaAllocator)) { throw new InvalidOperationException("allocator must be a CUDA allocator"); } var cudaAllocator = (CudaAllocator)allocator; using (var dnn = cudaAllocator.Context.DNNForDevice(cudaAllocator.DeviceId)) { var convDesc = GetConvDescriptor(cd, w.ElementType); using (var wDesc = GetFilterDescriptor(w)) using (var dyDesc = GetDescriptor(dy)) using (var dxDesc = GetDescriptor(dx)) { return(dnn.Value.GetConvolutionBackwardDataWorkspaceSize( wDesc, dyDesc, convDesc, dxDesc, (cudnnConvolutionBwdDataAlgo)algo)); } } }
public PagedObjectPool(IAllocator allocator) { _memory = allocator; _objectMap = new List <NativeArray <T> >(); _freeIds = new NativeStack <uint>(_memory, OBJECTS_PER_POOL); AddPage(); }
public static AllocationHandle Take <T>(this IAllocator it, int count) where T : unmanaged { var size = SizeOf <T> .Size; return(it.Take(size * count)); }
public FillExpression(IAllocator allocator, DType elementType, long[] sizes, Action <Tensor> fillAction) { this.allocator = allocator; this.elementType = elementType; this.sizes = sizes; this.fillAction = fillAction; }
// Constructs a convolutional network with two convolutional layers, // two fully-connected layers, ReLU units and a softmax on the output. public static void BuildCnn(IAllocator allocator, SeedSource seedSource, int batchSize, bool useCudnn, out Sequential model, out ICriterion criterion, out bool outputIsClassIndices) { var inputWidth = MnistParser.ImageSize; var inputHeight = MnistParser.ImageSize; var elementType = DType.Float32; var inputDims = new long[] { batchSize, 1, inputHeight, inputWidth }; model = new Sequential(); model.Add(new ViewLayer(inputDims)); var outSize = AddCnnLayer(allocator, seedSource, elementType, model, inputDims, 20, useCudnn); outSize = AddCnnLayer(allocator, seedSource, elementType, model, outSize, 40, useCudnn); var convOutSize = outSize[1] * outSize[2] * outSize[3]; model.Add(new ViewLayer(batchSize, convOutSize)); var hiddenSize = 1000; var outputSize = 10; model.Add(new DropoutLayer(allocator, seedSource, elementType, 0.5f, batchSize, convOutSize)); model.Add(new LinearLayer(allocator, seedSource, elementType, (int)convOutSize, hiddenSize, batchSize)); model.Add(new ReLULayer(allocator, elementType, batchSize, hiddenSize)); model.Add(new DropoutLayer(allocator, seedSource, elementType, 0.5f, batchSize, hiddenSize)); model.Add(new LinearLayer(allocator, seedSource, elementType, hiddenSize, outputSize, batchSize)); model.Add(LayerBuilder.BuildLogSoftMax(allocator, elementType, batchSize, outputSize, useCudnn)); criterion = new ClassNLLCriterion(allocator, batchSize, outputSize); outputIsClassIndices = true; // output of criterion is class indices }
public Allocation(IAllocator allocator, int offset, int count) { _allocator = allocator; Offset = offset; Count = count; }
public EntityChunkList(ILoggerFactory logFactory, IAllocator allocator, EntitySpec specification, int specIndex) { _logFactory = logFactory; _logger = logFactory.CreateLogger <EntityChunkList>(); _allocator = allocator; Specification = specification; SpecIndex = specIndex; }
protected static IColumnStream CreateColumnStream(ICodecFullStream codec, IAllocator allocator, int bufLen) { return(new ColumnStreamFullStream <ColumnMemoryStream, ICodecFullStream>( new ColumnMemoryStream(), codec, allocator, bufLen)); }
public void Dispose() { if (Old != null) { Allocator.SetAllocator(Old); } Old = null; }
public AllocatorBuffer(IAllocator <A, P, U> allocator, int blockLength, int index, int initialCounts = 100) { this.initialCounts = initialCounts; this.blockLength = blockLength; this.totalBlocks = 0; this.Index = index; this.allocator = allocator; }
//TODO: Should we really clear to zero by default? //the EntityChunk should be managing out of bounds indexing and moving of data public StackAllocator(IAllocator allocator, int size, bool thrash = false) { _allocator = allocator; _handle = allocator.Take(size); _free = (uint)size; _dataPtr = _handle.Address; _thrash = thrash; }
public Conv2Cuda(IAllocator allocator, SeedSource seedSource, DType elementType, int batchSize, int inputWidth, int inputHeight, int nInputPlane, int nOutputPlane, ConvolutionDesc2d cd) : base(allocator, seedSource, elementType, batchSize, inputWidth, inputHeight, nInputPlane, nOutputPlane, cd) { var finputSizes = TensorSharp.CUDA.SpatialConvolution.FInputSize(inputSizes, outputSizes, cd); this.finput = new NDArray(allocator, elementType, finputSizes); this.fgradInput = new NDArray(allocator, elementType, finputSizes); }
/// <summary> /// Converts to device. /// </summary> /// <param name="device">The device.</param> /// <returns>TVar.</returns> public NDArray ToDevice(IAllocator device) { if (Storage.Allocator.GetType().Name == device.GetType().Name) { return(this); } return(new Variable(new ToDeviceExpression(this.TVar().Expression, device)).Evaluate()); }
public CudaStorage(IAllocator allocator, TSCudaContext tsContext, CudaContext context, DType ElementType, long elementCount) : base(allocator, ElementType, elementCount) { TSContext = tsContext; this.context = context; bufferHandle = tsContext.AllocatorForDevice(DeviceId).Allocate(ByteLength); deviceBuffer = bufferHandle.Pointer; }
public RenderCommandBuffer(IAllocator allocator, GraphicsDevice device) { _buffer = new NativeBuffer(allocator); _device = device; _defaultEffect = new BasicEffect(_device); _defaultEffect.TextureEnabled = true; _defaultEffect.VertexColorEnabled = true; }
public static Compiler Create(Block romData, IAllocator allocator, IEnumerable <IModule> modules) { Compiler compiler = new Compiler(); compiler._romData = romData; compiler._allocator = allocator; compiler._modules = modules; return(compiler); }
public void Setup() { _logFactory = LoggerFactory.Create(builder => { builder.AddConsole(); }); _memory = new HeapAllocator(_logFactory); }
public SystemManager(ILoggerFactory logFactory, EntityManager em, IAllocator allocator) { _logFactory = logFactory; _logger = logFactory.CreateLogger <SystemManager>(); _entityManager = em; _allocator = allocator; DefaultStage = "Default"; _variables = new NativeBuffer(allocator); }
public EntityPool(ILoggerFactory logFactory, IAllocator allocator) { _logFactory = logFactory; _logger = _logFactory.CreateLogger <EntityPool>(); _memory = allocator; _freeIds = new NativeStack <uint>(_memory, ENTITIES_PER_POOL); _entityMap = new List <NativeArray <Entity> >(); //AddPage(); Take(); }
public TimeSeriesRecorder( string symbol, ITimeSeriesDbUpdater dbUpdater, IAllocator allocator) { _symbol = symbol; _dbUpdater = dbUpdater; _allocator = allocator; _buffer = allocator.Allocate(Natives.MAX_ENTRY_SIZE); }
public BodyCollisionFunctor(PhysicsManager manager) { _manager = manager; _taskManager = TaskManager.Current; int threads = TaskManager.ThreadCount; _alloc = new IAllocator<ContactConstraint>[threads]; _items = new List<ContactConstraint>[threads]; _contact = new ContactConstraint[threads]; _a = new Part[threads]; _b = new Part[threads]; for (int i = 0; i < threads; i++) { _alloc[i] = new ContactConstraint.Allocator(manager.Game, manager.ContactPoolCapacity, 2, manager.MaxPointsPerContact); _items[i] = new List<ContactConstraint>(); } _fastSpeedSquared = manager.SweepThresholdSquared; }
private static ManagedPointer Insert(Bitmap toInsert, Color[] palette, int blockAdded, bool compressed, IROM rom, IAllocator<ManagedPointer> allocator) { var bytes = GBAGraphics.ToGBARaw(toInsert, palette, GraphicsMode.Tile4bit); int insertedLength; int bytesAdded = blockAdded * 32; if (compressed) { bytes = LZ77.Compress(bytes, 0, bytes.Length - bytesAdded); insertedLength = bytes.Length; } else { insertedLength = bytes.Length - bytesAdded; } var ptr = allocator.Allocate(insertedLength, 4); if (!ptr.IsNull) { rom.WriteData(ptr, bytes, 0, insertedLength); } return ptr; }
public bool CanAllocate(IAllocator allocator, Specification spec) { return spec.ResourceType == "Folder" && Directory.Exists("C:\\Workspace"); }
public async Task<IResource> Allocate(IAllocator allocator, Specification spec) { var directory = new DirectoryInfo("C:\\Workspace"); return new FolderResource(directory.CreateSubdirectory("FolderAllocation_" + RandomString())); }
public bool CanLease(IAllocator allocator, Specification spec) { return spec.ResourceType == "Folder" && !LeasedOut; }
protected BaseAllocatorTest(IAllocator allocator) { _allocator = allocator; }
/// <summary> /// Initialize the physics implementation. /// </summary> public override void Initialize() { base.Initialize(); if (_broadPhase == null) _broadPhase = new SweepAndPrune(); _islandAlloc = new Pool<Island>(_islandPoolCapacity, 2); _contacts = new BodyCollisionFunctor(this); _contactCache = new ContactCache(new ContactCache.Allocator(_contactPoolCapacity, 2, _maxPointsPerContact)); _taskManager = TaskManager.Current; _generators.Add(_gravityForce); }
public async Task<ILease> Lease(IAllocator allocator, Specification spec) { LeasedOut = true; _folderLease = new FolderLease(this, Directory); return _folderLease; }
public PythonMapper(PythonContext python, string stubPath, IAllocator allocator) { this.Init(python, stubPath, allocator); }
public PythonMapper(CodeContext context, string stubPath, IAllocator allocator) : this(context.LanguageContext, stubPath, allocator) { }
public PythonMapper(CodeContext context, IAllocator allocator) : this(context, null, allocator) { }
/// <summary> /// /// </summary> /// <param name="picture"></param> /// <param name="allocator"></param> /// <param name="rom"></param> /// <returns> /// first is main portrait offset, /// second is miniportrait, /// third is palette and /// fourth is mouth frames if separate /// </returns> public static CanCauseError<ManagedPointer[]> WriteData(Bitmap picture, IAllocator<ManagedPointer> allocator, IROM rom) { var format = GetFormat(rom.GameCode); return WriteData(format, picture, allocator, rom); }
/// <summary> /// /// </summary> /// <param name="picture"></param> /// <param name="format"></param> /// <param name="allocator"></param> /// <param name="rom"></param> /// <returns> /// first is main portrait offset, /// second is miniportrait, /// third is palette and /// fourth is mouth frames if separate /// </returns> private static CanCauseError<ManagedPointer[]> WriteData(PortraitFormat format, Bitmap picture, IAllocator<ManagedPointer> allocator, IROM rom) { List<ManagedPointer> pointers = new List<ManagedPointer>(4); //Get palette Color[] palette; bool palettedBitmap = picture.PixelFormat.HasFlag(PixelFormat.Indexed); if (!palettedBitmap) { var colors = picture.GetColors(); colors = GBAPalette.GetGBAColors(colors); if (colors.Count > 16) { return CanCauseError<ManagedPointer[]>.Error("Over 16 colours."); } palette = colors.ToArray(); } else { palette = picture.Palette.Entries; } //Split to separate portraits Bitmap mainPortrait = new Bitmap( format.PortraitSize.Width, format.PortraitSize.Height, picture.PixelFormat); Move(picture, mainPortrait, format.PictureMapping); Bitmap mouthbm; if (format.SeparateMouthFrames) { mouthbm = new Bitmap(format.MouthSize.Width, format.MouthSize.Height, picture.PixelFormat); } else { mouthbm = mainPortrait; } Move(picture, mouthbm, format.MouthMapping); Bitmap mini = new Bitmap(format.MiniSize.Width, format.MiniSize.Height, picture.PixelFormat); Move(picture, mouthbm, format.MiniMapping); //Write data pointers.Add(Insert(mainPortrait, palette, format.BlockAdd, format.CompressedPortrait, rom, allocator)); pointers.Add(Insert(mini, palette, 0, format.CompressedMini, rom, allocator)); byte[] rawPalette = GBAPalette.toRawGBAPalette(palette); var ptr = allocator.Allocate(rawPalette.Length, 4); if (!ptr.IsNull) { rom.WriteData(ptr, rawPalette, 0, rawPalette.Length); } pointers.Add(ptr); if (mouthbm != mainPortrait) { pointers.Add(Insert(mouthbm, palette, 0, true, rom, allocator)); } return pointers.ToArray(); }