static void startTransmit(unsigned epNum, struct transmitRequest_t *tx) { unsigned total = fillOutput(epNum,tx); if( 0 == ( UDP->UDP_CSR[epNum] & AT91C_UDP_TXPKTRDY ) ){ UDP_SETEPFLAGS(UDP->UDP_CSR+epNum, AT91C_UDP_TXPKTRDY ); connections[epNum].txState = EPTX_WAITCOMP ; if( USBEP_BULKIN == epNum ) total += fillOutput(epNum,tx); if( 0 )// != epNum ) { NODEBUGMSG( "extra 0x" ); NODEBUGHEX( total ); NODEBUGMSG( "\r\n" ); } } else { write( DEFUART, "PKTRDY already set for ep 0x" ); writeHexChar( DEFUART, epNum ); write( DEFUART, "\r\n" ); } DEBUGMSG( "~txPacket: 0x" ); DEBUGHEX(tx->txNext->length-tx->txNext->offset); DEBUGMSG( "\r\n" ); }
int main(int argc, char* argv[]) { if(argc != 2) { std::cerr << "Usage : " << argv[0] << "<0,1>" <<std::endl; std::cerr << "with : " << std::endl; std::cerr << "0 : quadratic loss" << std::endl; std::cerr << "1 : cross entropy loss" << std::endl; return -1; } bool quadratic_loss = (atoi(argv[1]) == 0); srand(time(NULL)); // We compare our computation of the gradient to // a finite difference approximation // The loss is also involved std::cout << "---------------------------------" << std::endl; std::cout << "Comparing the analytical gradient and numerical approximation " << std::endl; auto input = gaml::mlp::input<X>(INPUT_DIM, fillInput); auto l1 = gaml::mlp::layer(input, HIDDEN_LAYER_SIZE, gaml::mlp::mlp_sigmoid(), gaml::mlp::mlp_dsigmoid()); auto l2 = gaml::mlp::layer(l1, HIDDEN_LAYER_SIZE, gaml::mlp::mlp_identity(), gaml::mlp::mlp_didentity()); auto l3 = gaml::mlp::layer(l2, HIDDEN_LAYER_SIZE, gaml::mlp::mlp_tanh(), gaml::mlp::mlp_dtanh()); auto l4 = gaml::mlp::layer(l3, OUTPUT_DIM, gaml::mlp::mlp_sigmoid(), gaml::mlp::mlp_dsigmoid()); auto mlp = gaml::mlp::perceptron(l4, output_of); std::cout << "We use the following architecture : " << std::endl; std::cout << mlp << std::endl; std::cout << "which has a total of " << mlp.psize() << " parameters"<< std::endl; gaml::mlp::parameters_type params(mlp.psize()); gaml::mlp::parameters_type paramsph(mlp.psize()); gaml::mlp::values_type derivatives(mlp.psize()); gaml::mlp::values_type forward_sweep(mlp.size()); X x; auto loss_ce = gaml::mlp::loss::CrossEntropy(); auto loss_quadratic = gaml::mlp::loss::Quadratic(); auto f = [&mlp, ¶ms] (const typename decltype(mlp)::input_type& x) -> gaml::mlp::values_type { auto output = mlp(x, params); gaml::mlp::values_type voutput(mlp.output_size()); fillOutput(voutput.begin(), output); return voutput; }; auto df = [&mlp, &forward_sweep, ¶ms] (const typename decltype(mlp)::input_type& x, unsigned int parameter_dim) -> gaml::mlp::values_type { return mlp.deriv(x, params, forward_sweep, parameter_dim); }; unsigned int nbtrials = 100; unsigned int nbfails = 0; std::cout << "I will compare " << nbtrials << " times a numerical approximation and the analytical gradient we compute" << std::endl; for(unsigned int t = 0 ; t < nbtrials ; ++t) { randomize_data(params, -1.0, 1.0); randomize_data(x, -1.0, 1.0); // Compute the output at params auto output = mlp(x, params); gaml::mlp::values_type raw_output(OUTPUT_DIM); fillOutput(raw_output.begin(), output); gaml::mlp::values_type raw_outputph(OUTPUT_DIM); // For computing the loss, we need a target gaml::mlp::values_type raw_target(OUTPUT_DIM); randomize_data(raw_target); double norm_dh = 0.0; for(unsigned int i = 0 ; i < mlp.psize() ; ++i) { // Let us compute params + h*[0 0 0 0 0 0 1 0 0 0 0 0], the 1 at the ith position std::copy(params.begin(), params.end(), paramsph.begin()); double dh = (sqrt(DBL_EPSILON) * paramsph[i]); paramsph[i] += dh; norm_dh += dh*dh; // Compute the output at params + h auto outputph = mlp(x, paramsph); fillOutput(raw_outputph.begin(), outputph); // We now compute the approximation of the derivative if(quadratic_loss) derivatives[i] = (loss_quadratic(raw_target, raw_outputph) - loss_quadratic(raw_target, raw_output))/dh; else derivatives[i] = (loss_ce(raw_target, raw_outputph) - loss_ce(raw_target, raw_output))/dh; } // We now compute the analytical derivatives mlp(x, params); std::copy(mlp.begin(), mlp.end(), forward_sweep.begin()); gaml::mlp::values_type our_derivatives(mlp.psize()); for(unsigned int i = 0 ; i < mlp.psize() ; ++i) { if(quadratic_loss) our_derivatives[i] = loss_quadratic.deriv(x, raw_target, forward_sweep, f, df, i); else our_derivatives[i] = loss_ce.deriv(x, raw_target, forward_sweep, f, df, i); } // We finally compute the norm of the difference double error = 0.0; auto diter = derivatives.begin(); for(auto& ourdi : our_derivatives) { error = (ourdi - *diter) * (ourdi - *diter); diter++; } error = sqrt(error); std::cout << "Error between the analytical and numerical gradients " << error << " with a step size of " << sqrt(norm_dh) << " in norm" << std::endl; if(error > 1e-7) ++nbfails; /* std::cout << "numerical " << std::endl; for(auto & di : derivatives) std::cout << di << " "; std::cout << std::endl; std::cout << "our :" << std::endl; for(auto& di : our_derivatives) std::cout << di << " "; std::cout << std::endl; */ } std::cout << nbfails << " / " << nbtrials << " with an error higher than 1e-7" << std::endl; }
size_t BitmapEncoding::Bitmap::compress(void* dst, const ConstChunk& chunk, size_t chunkSize) { char const* dataSrc = (char const*)chunk.getData(); TypeId type = chunk.getAttributeDesc().getType(); _elementSize = TypeLibrary::getType(type).byteSize(); /* No more immutable arrays, to keep consistent with old code, always treat data as string */ _bitmapElements = chunkSize; _elementSize = 1; if(!_bitmapElements) { return chunkSize; } char *readPos = const_cast<char *>(dataSrc); ByteOutputItr out((uint8_t *) dst, chunkSize-1); uint32_t i; uint32_t bucketSize = (_bitmapElements + 7) >> 3; uint32_t bucketCount = 0; std::string key; clearBitmapCache(); // make the key of our hash a string so that // we can compare variable-length element sizes size_t bitmapEntryLength = bucketSize + _elementSize; assert(bitmapEntryLength); uint32_t maxBuckets = floor(chunkSize / bitmapEntryLength); if(maxBuckets * bitmapEntryLength == chunkSize) { // we want to beat the uncompressed case --maxBuckets; } for(i = 0; i < _bitmapElements; ++i) { key.clear(); for(uint32_t j = 0; j < _elementSize; ++j) { key.push_back(*readPos); ++readPos; } uint8_t *bucket = NULL; // check to see if a bucket exists, if so grab and pass on std::map<std::string, uint8_t*>::iterator iter = _bitmaps.find(key); if(iter == _bitmaps.end() ) { ++bucketCount; if(bucketCount > maxBuckets) { return chunkSize; } // create a new one bucket = new uint8_t[bucketSize]; _bitmaps[key] = bucket; for(uint32_t k = 0; k < bucketSize; ++k) { *(bucket+k) = 0;} } else { bucket = iter->second; } assert(bucket!=NULL); setBit(bucket, i); } // drop all of bitmaps to dst fillOutput(&out); size_t compressedSize = out.close(); return compressedSize; }